package com.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dk.pojo.Menu;
import com.dk.pojo.TPerson;
import com.dk.service.MenuServiceI;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("menuController")
public class MenuController {

    //角色
    @Reference //引用dubbo服务接口，不能使用  @Autowired
    private MenuServiceI menuServiceI;


    @RequestMapping("list")
    public String listGet(){
        return "/resources/list";
    }

    //查询权限 树
    @RequestMapping("getTSource")
    @ResponseBody
    public List<Map<String,Object>> getTSource(){
        TPerson tPerson = (TPerson)SecurityUtils.getSubject().getPrincipal();
        List<Map<String,Object>> list = menuServiceI.getTSource(tPerson);
        return list;
    }

    //跳转ztree
    @RequestMapping("listGet")
    public String listZtree(){
        return "/resources/showTree";
    }

    //查询并返回ztree
    @RequestMapping("getzTree")
    @ResponseBody
    public List<Map<String,Object>> getzTree(){
        List<Map<String,Object>> list = menuServiceI.getzTree();
        return list;
    }

    //跳转增加Ztree
    @RequestMapping("addInfo")
    public String addInfo(){
        return "resources/add";
    }

    //确认增加
    @RequestMapping("addInfos")
    @ResponseBody
    public int  addInfos(Menu menu){
        System.out.println(menu.toString());
        TPerson tPerson = (TPerson)SecurityUtils.getSubject().getPrincipal();
        menuServiceI.addInfos(menu,tPerson);
        return 1;
    }
    //回显
    @RequestMapping("getUpdateInfo/{id}")
    public String getUpdateInfo(@PathVariable("id") Integer id, Model model){
        Menu menu = menuServiceI.getUpdateInfo(id);
        model.addAttribute("menu",menu);
        return "resources/update";
    }

    //确认修改
    @RequestMapping("updateInfo")
    @ResponseBody
    public int updateInfo(Menu menu){
        System.out.println(menu.getButton());
        TPerson tPerson = (TPerson)SecurityUtils.getSubject().getPrincipal();
        menuServiceI.updateInfo(menu,tPerson);
        return 1;
    }

    //删除
    @RequestMapping("deleteInfo/{id}")
    @ResponseBody
    public int deleteInfo(@PathVariable("id")Integer id){
        TPerson tPerson = (TPerson)SecurityUtils.getSubject().getPrincipal();
        menuServiceI.deleteInfo(id,tPerson);
        return 1;
    }

    //根据角色id 去查权限
    @RequestMapping("getzCheckedTree/{roleId}")
    @ResponseBody
    public List<Map<String,Object>> getzCheckedTree(@PathVariable("roleId")Integer roleId){
        List<Map<String,Object>> list = menuServiceI.getzCheckedTree(roleId);
        return list;
    }
}
